<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Common\Model;
use Think\Model;


class SmsModel extends Model{

    /* 用户模型自动完成 */
    protected $_auto = array(

        array('status', 1, self::MODEL_INSERT),
        array('created_at', NOW_TIME, self::MODEL_INSERT),
    );




    public function send($mobile){
        if (isMobile($mobile)==false){
            return ['status'=>false,'message'=>'手机号码格式不正确'];
        }
        $model = M('Sms');
        $map = array();
        $map['mobile']      =   $model;
        $time = NOW_TIME - 120;

        $count = $model->where("created_at>=$time")->count();
        if(!empty($count)){
            return ['status'=>false,'message'=>'你发送太频繁，请稍后在试！'];
        }
        $captcha = rand(100000,999999);
        $content = "你的验证码是{$captcha}，请勿泄露他人，请在十分钟内使用。";
        $api = new \Common\Extend\Api\ApiClient();
        $api = $api->sms();
        $api->mobile       = $mobile;
        $api->content      = $content;
        $error =$api->Send('get')->data();
        $trl = $api->Error($error);
        if($trl['status']){
            $data = [
                'mobile'    => $mobile,
                'captcha'   => $captcha,
                'created_at'=> NOW_TIME,
                'status'=> 1,
            ];
            if($model->add($data)){
                return ['status'=>true,'info'=>'发送成功'];
            }else{
                return ['status'=>false,'info'=>'提交内容出错'];
            }
        }else{
            return $trl;
        }
    }

    public function getsms($mobile,$captcha){
        if (isMobile($mobile)==false){
            return ['status'=>false,'info'=>'手机号码格式不正确'];
        }elseif(strlen($captcha)!=6){
            return ['status'=>false,'info'=>'验证码格式不正确'];
        }
        $model = D('Sms');
        $map = array();
        $map['mobile']      =   $mobile;
        $map['captcha']     =   $captcha;
        $map['status']      =   1;
        $info = $model->where($map)->find();
        if(empty($info)){
            return ['status'=>false,'info'=>'验证码不正确'];
        }
        $time = NOW_TIME - 600;
        if($info['created_at']<$time){
            return ['status'=>false,'info'=>'验证码已过期'];
        }else{
            $model->where('id='.$info['id'])->save(['status'=>0]);
            return ['status'=>true,'info'=>'验证成功'];
        }

    }

    public function GetError(){
        return [
            0 =>'提交成功',
            1 =>'含有敏感词汇',
            2 =>'余额不足',
            3 =>'没有号码',
            4 =>'包含sql语句',
            10 =>'账号不存在',
            11 =>'账号注销',
            12 =>'账号停用',
            13 =>'IP鉴权失败',
            14 =>'格式错误',
            -1 =>'系统异常',
        ];
    }

}
